package cn.tannn.cat.file.server.controller.manage;

import cn.tannn.cat.file.server.controller.manage.dto.StorageBucketAppend;
import cn.tannn.cat.file.server.controller.manage.dto.StorageBucketPage;
import cn.tannn.cat.file.server.entity.StorageBucket;
import cn.tannn.cat.file.server.service.StorageBucketService;
import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.exception.annotation.DisposeException;
import cn.tannn.jdevelops.jpa.constant.SQLOperator;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 存储桶信息
 *
 * @author <a href="https://t.tannn.cn/">tnnn</a>
 * @version V1.0
 * @date 2025/6/2 13:49
 */
@Tag(name = "存储桶信息", description = "存储桶信息")
@PathRestController("bucket")
@RequiredArgsConstructor
@Slf4j
@Valid
public class StorageBucketController {

    private final StorageBucketService storageBucketService;


    @Operation(summary = "新增存储桶")
    @PostMapping("append")
    @DisposeException(messages = "当前桶已存在请勿重复添加")
    public ResultVO<String> append(@RequestBody @Valid StorageBucketAppend append) {
        storageBucketService.saveOne(append.toStorageBucket());
        return ResultVO.successMessage("存储桶新增成功");
    }

    @Operation(summary = "查询存储桶-分页")
    @PostMapping("page")
    public ResultPageVO<StorageBucket, JpaPageResult<StorageBucket>> page(@RequestBody @Valid StorageBucketPage page) {
        Page<StorageBucket> buckets = storageBucketService.findPage(page, page.getPage());
        JpaPageResult<StorageBucket> result = JpaPageResult.toPage(buckets);
        return ResultPageVO.success(result, "查询成功");
    }

    @Operation(summary = "根据存储器查询桶")
    @GetMapping("list/{configId}")
    public ResultVO<List<StorageBucket>> list(@PathVariable("configId") String configId) {
        if(configId.equals("all")){
            return ResultVO.success(storageBucketService.finds());
        }
        return ResultVO.success(storageBucketService.finds("configId", SQLOperator.EQ, configId));
    }

    @Operation(summary = "删除桶")
    @DeleteMapping("delete/{id}")
    public ResultVO<String> deletes(@PathVariable("id") Long id) {
        storageBucketService.deleteEq("id", id);
        return ResultVO.successMessage("所选桶删除成功");
    }
}
